
from datetime import date
import json
import os
import cv2
import numpy as np

# 制作数据集
txt_name = 'data/All_data.txt'
txt_file = open(txt_name, 'w',encoding='utf-8')         
 
def decode_json(json_floder_path,json_name):

    json_path = os.path.join(json_floder_path, json_name)
    with open(json_path, 'r') as f:
        data = json.load(f)
    # img_w = data['imageWidth']
    # img_h = data['imageHeight']

    for s in data['shapes']:
        n = len(s['points'])
        if len(s['points']) ==65:
            # print(len(s['points']))
            # print(json_name)
            pts =[]
            pts2 = []
            for i in range(len(s['points'])):
                x = int(s['points'][i][0])
                y = int(s['points'][i][1])
                pts.append(x)
                pts.append(y)
                pts2.append([x,y])

            if pts:
                # print(np.array(pts2))
                x, y, w, h = cv2.boundingRect(np.array(pts2))  #矩形边框（Bounding Rectangle）是说，用一个最小的矩形，把找到的形状包起来
                if pts2[0][0]-pts2[-1][0] > 300:
                    pts2.reverse()
                    pts = [pts2[i][j] for i in range(len(pts2)) for j in range(2)]
                #增加区域限制，统一后续切割为860*340区域 归一化
                # if w<=760 and h<=240:
                #     w=760
                #     h=240
                #     txt_file.write(json_path + "," + str(x - 30) + " " + str(y - 30) + " " + str(w+60) + " " + str(h+60) + ",")
                #     txt_file.write(",".join([str(a) for a in pts]))
                #     txt_file.write('\n')

            #不统一化
            txt_file.write(json_path + "," + str(x - 30) + " " + str(y - 30) + " " + str(w+60) + " " + str(h+60) + ",")
            txt_file.write(",".join([str(a) for a in pts]))
            txt_file.write('\n')
    
if __name__ == "__main__":
    json_floder = r'D:\qudian\datamake_qudian\test'
    json_names = os.listdir(json_floder)
    for json_name in json_names:
        if json_name.endswith('.json'):
            decode_json(json_floder,json_name)
    txt_file.close()
